package cn.yzstu.second;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.buffer.impl.BufferImpl;
import io.vertx.core.file.FileSystem;

/**
 * @author Baldwin
 */
public class FileWrite extends AbstractVerticle {

    public static void main(String[] args) {
        Vertx vertx = Vertx.vertx();
        vertx.deployVerticle(FileWrite.class.getName());
    }

    @Override
    public void start() throws Exception {
        FileSystem fs = vertx.fileSystem();
        Future<Void> startFuture = Future.future();

        Future<Void> fut1 = Future.future();
        fs.createFile("/foo", fut1.completer());

        fut1.compose(v -> {
            Buffer buffer = Buffer.buffer("hello world");
            // fut1中文件创建完成后执行
            Future<Void> fut2 = Future.future();
            fs.writeFile("/foo", buffer, fut2.completer());
            return fut2;
        }).compose(v -> {
                    // fut2文件写入完成后执行
                    fs.move("/foo", "/bar", startFuture.completer());
                },
                // 如果任何一步失败，将startFuture标记成failed
                startFuture);
    }

}
